import allure
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

class keywords:
    # 初始化
    def __init__(self,driver):
        self.driver = driver
        self.wait = WebDriverWait(self.driver,10)

    # 打开浏览器
    @allure.step("打开浏览器")
    def open(self,url):
        self.driver.get(url)


    # 定位元素
    @allure.step("修饰元素")
    def locator_station(self,element):
        self.driver.execute_script(
            "arguments[0].setAttribute('style',arguments[1]);",
            element,
            "border:2px solid red"
        )
    @allure.step("元素定位")
    def locator_with_wait(self,name,value):
        locator = (name,value)
        return self.wait.until(ec.visibility_of_element_located(locator))

    @allure.step("多元素定位")
    def locators_with_wait(self,name,value):
        locator = (name,value)
        el = self.wait.until(ec.visibility_of_element_located(locator))
        self.locator_station(el)
        return el

    @allure.step("点击元素")
    def on_click(self,name,value):
        # locator = (name,value)
        self.locator_with_wait(name,value).click()

    @allure.step("元素定位")
    def locator_with_wait(self,name,value):
        locator = (name,value)
        el = self.wait.until(ec.visibility_of_element_located(locator))
        self.locator_station(el)
        return el

    @allure.step("窗口切换")
    def change_windows(self,n):
        handles = self.driver.window_handles
        self.driver.switch_to.window(handles[n])


    # 获得结果
    @allure.step("获得返回结果")
    def text_wait(self,name,value,txt):
        el = (name,value)
        result = self.wait.until(ec.text_to_be_present_in_element(el,txt))
        return result

    # 获得文本内容结果
    @allure.step("获得返回文本")
    def get_text(self,name,value):
        el = (name,value)
        result = self.wait.until(
            lambda x:x.find_element(*el).text
        )
        return result
































